package com.gitee.fsmxm.ocon.util;

import com.gitee.fsmxm.ocon.constant.AppEnum;
import com.gitee.fsmxm.ocon.core.ConvertException;
import com.gitee.fsmxm.ocon.model.Procedure;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author fsmxm
 */
public class ProcedureUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProcedureUtils.class);

    private ProcedureUtils() {
    }

    public static Procedure create(String application, String appName) throws ConvertException {
        ActiveXComponent app = null;
        Dispatch dispatch = null;
        try {
            app = new ActiveXComponent(application);
            final String name = appName.substring(0, appName.indexOf('.'));
            final AppEnum appEnum = AppEnum.getInstance(name);
            if (appEnum != AppEnum.POWERPOINT) {
                app.setProperty("Visible", false);
            }
            if (appEnum == AppEnum.EXCEL) {
                // 不显示弹框，如另存为弹出兼容性检测、文件已存在是否覆盖的弹框
                app.setProperty("DisplayAlerts", false);
            }
            dispatch = app.getProperty(AppUtils.getDispatch(appName)).toDispatch();
        } catch (Exception e) {
            throw new ConvertException("error create Procedure application=" + application, e);
        }
        return new Procedure(app, dispatch);
    }

}
